LCR 034. 验证外星语词典
为保证权益,题目请参考 LCR 034. 验证外星语词典(From LeetCode).
解决方案1
Python
python
from typing import List
class Solution:
def isAlienSorted(self, words: List[str], order: str) -> bool:
order_map = dict([(c, i) for i, c in enumerate(order)])
def my_cmp(a: str, b: str):
# a < b
an = len(a)
bn = len(b)
n = max(an, bn)
for i in range(n):
if i >= an and i < bn:
break
if i <= an and i >= bn:
return False
if a[i] == b[i]:
continue
ao = order_map[a[i]]
bo = order_map[b[i]]
if ao < bo:
break
return False
return True
for i in range(len(words) - 1):
if my_cmp(words[i], words[i + 1]) == False:
return False
return True
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40